package cn.school.service.impl;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import cn.school.dto.FindObjectScoreDto;
import cn.school.dto.ObjectScoreDto;
import cn.school.mapper.ExamPaperMapper;
import cn.school.mapper.PaperSubmissionMapper;
import cn.school.mapper.ScoreSubjectMapper;
import cn.school.mapper.ScoreTotalMapper;
import cn.school.pojo.*;
import cn.school.service.ScoreSubjectService;
import cn.school.service.TeacherService;
import cn.school.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【score_subject】的数据库操作Service实现
* @createDate 2025-06-11 20:05:15
*/
@Service
public class ScoreSubjectServiceImpl extends ServiceImpl<ScoreSubjectMapper, ScoreSubject>
    implements ScoreSubjectService {
    @Resource
    private ScoreSubjectMapper scoreSubjectMapper;
    @Resource
    private TeacherService teacherService;
    @Resource
    private PaperSubmissionMapper paperSubmissionMapper;
    @Resource
    private ExamPaperMapper examPaperMapper;
    @Resource
    private ScoreTotalMapper scoreTotalMapper;
    @Override
    public String setSubjectScore(ObjectScoreDto objectScoreDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);
        if(teacher==null){
            return "您不是教师";
        }else {
            QueryWrapper<ExamPaper> examPaperQueryWrapper = new QueryWrapper<>();
            examPaperQueryWrapper.eq("id",objectScoreDto.getExamPaperId());
            ExamPaper examPaper = examPaperMapper.selectOne(examPaperQueryWrapper);
            Long subjectId = examPaper.getSubjectId();
            QueryWrapper<PaperSubmission> paperSubmissionQueryWrapper = new QueryWrapper<>();
            paperSubmissionQueryWrapper.eq("exam_paper_id",objectScoreDto.getExamPaperId());
            paperSubmissionQueryWrapper.eq("student_id",objectScoreDto.getStudentId());
            List<PaperSubmission> paperSubmissions = paperSubmissionMapper.selectList(paperSubmissionQueryWrapper);
            Double score = 0.0;
            for (PaperSubmission paperSubmission : paperSubmissions) {
                score += paperSubmission.getScore();
            }
            ScoreSubject scoreSubject = new ScoreSubject();
            scoreSubject.setStudentId(Long.valueOf(objectScoreDto.getStudentId()));
            scoreSubject.setSubjectId(subjectId);
            scoreSubject.setSubjectScore(score);
            scoreSubject.setIsDel(0);
            scoreSubject.setSubmisssionTime(new Date());
            scoreSubject.setSessionId(objectScoreDto.getExamSessionId());
            scoreSubject.setPaperId(Long.valueOf(objectScoreDto.getExamPaperId()));
            scoreSubjectMapper.insert(scoreSubject);
            QueryWrapper<ExamPaper> examPaperQueryWrapper1 = new QueryWrapper<>();
            examPaperQueryWrapper1.eq("session_id",objectScoreDto.getExamPaperId());
            List<ExamPaper> examPapers = examPaperMapper.selectList(examPaperQueryWrapper1);
            List<Long> list=new ArrayList<>();
            for (ExamPaper examPaper1 : examPapers) {
                list.add(examPaper1.getId());
            }
            QueryWrapper<ScoreTotal> totalQueryWrapper = new QueryWrapper<>();
            totalQueryWrapper.eq("student_id",objectScoreDto.getStudentId());
            totalQueryWrapper.eq("session_id",objectScoreDto.getExamSessionId());
            ScoreTotal scoreTotal = scoreTotalMapper.selectOne(totalQueryWrapper);
            if(scoreTotal==null){
                ScoreTotal scoreTotal1 = new ScoreTotal();
                scoreTotal1.setStudentId(Long.valueOf(objectScoreDto.getStudentId()));
                scoreTotal1.setSessionId(objectScoreDto.getExamSessionId());
                scoreTotal1.setTotalScore(score);
                scoreTotal1.setIsDel(0);
                scoreTotal1.setCreateTime(new Date());
                scoreTotal1.setUpdateTime(new Date());
                scoreTotalMapper.insert(scoreTotal1);
            }else {
                scoreTotal.setTotalScore(scoreTotal.getTotalScore()+score);
                scoreTotal.setUpdateTime(new Date());
                scoreTotalMapper.updateById(scoreTotal);
            }
            return "设置成功";
        }
    }

    @Override
    public ResultVo findObjectScore(FindObjectScoreDto findobjectScoreDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);
        if(teacher==null){
            return ResultVo.error("您不是教师,无权查看");
        }else {
            QueryWrapper<ScoreSubject> scoreSubjectQueryWrapper = new QueryWrapper<>();
            if(findobjectScoreDto.getStudentId()!=null){
                scoreSubjectQueryWrapper.like("student_id",findobjectScoreDto.getStudentId());
            }
            if(findobjectScoreDto.getSessionId()!=null){
                scoreSubjectQueryWrapper.eq("session_id",findobjectScoreDto.getSessionId());
            }
            scoreSubjectQueryWrapper.like("paper_id",findobjectScoreDto.getPaperId());
            scoreSubjectQueryWrapper.eq("is_del",0);
            scoreSubjectQueryWrapper.orderByDesc("subject_score");
            List<ScoreSubject> scoreSubjects = scoreSubjectMapper.selectList(scoreSubjectQueryWrapper);
            return ResultVo.ok(scoreSubjects);
        }
    }

    @Override
    public ResultVo findTotalScore(Integer sessionId, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherService.getOne(teacherQueryWrapper);
        if(teacher==null){
            return ResultVo.error("您不是教师,无权查看");
        }else {
            QueryWrapper<ScoreTotal> scoreTotalQueryWrapper = new QueryWrapper<>();
            scoreTotalQueryWrapper.eq("session_id",sessionId);
            scoreTotalQueryWrapper.eq("is_del",0);
            scoreTotalQueryWrapper.orderByDesc("total_score");
            List<ScoreTotal> scoreTotals = scoreTotalMapper.selectList(scoreTotalQueryWrapper);
            return ResultVo.ok(scoreTotals);
        }
    }
}




